package com.jirefox.loner.daemon.initialize;

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.jirefox.loner.daemon.model.WorkDir;
import com.jirefox.loner.daemon.service.AppService;

@Component
public class InitApps implements CommandLineRunner {

	private final Logger logger = LoggerFactory.getLogger(InitApps.class);

	@Autowired
	private WorkDir workDir;

	@Autowired
	private AppService appService;

	@Override
	public void run(String... args) throws Exception {

		File appsFolder = new File(workDir.appsDir());
		if (!appsFolder.exists()) {
			appsFolder.mkdirs();
		}

		for (File appFolder : appsFolder.listFiles()) {
			logger.info("start app {}", appFolder.getAbsolutePath());
			appService.startApp(appFolder);
		}

	}

}
